package leetcode.Y2024M7;

import org.junit.Test;

import java.util.*;

public class S17 {
    private static final Map<Character, List<String>> map = new HashMap<>();

    static {
        map.put('1', new ArrayList<>(Collections.singletonList("one")));
        map.put('2', new ArrayList<>(Arrays.asList("a", "b", "c")));
        map.put('3', new ArrayList<>(Arrays.asList("d", "e", "f")));
        map.put('4', new ArrayList<>(Arrays.asList("g", "h", "i")));
        map.put('5', new ArrayList<>(Arrays.asList("j", "k", "l")));
        map.put('6', new ArrayList<>(Arrays.asList("m", "n", "o")));
        map.put('7', new ArrayList<>(Arrays.asList("p", "q", "r", "s")));
        map.put('8', new ArrayList<>(Arrays.asList("t", "u", "v")));
        map.put('9', new ArrayList<>(Arrays.asList("w", "x", "y", "z")));
    }


    public List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        if (digits == null || digits.isEmpty()) {
            return res;
        }
        Queue<String> queue = new LinkedList<>();
        for (int i = 0; i < digits.length(); i++) {
            combineLetter(queue, map.get(digits.charAt(i)));
        }
        res.addAll(queue);
        return res;
    }

    private void combineLetter(Queue<String> queue, List<String> letters) {
        if (queue.isEmpty()) {
            queue.addAll(letters);
        } else {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                String s = queue.poll();
                for (String letter : letters) {
                    queue.add(s + letter);
                }
            }
        }

    }

    @Test
    public void test() {
       String digits = "23";
        System.out.println(letterCombinations(digits));
    }
}
